<?php
class MembersController extends AppController {

	var $name = 'Members';
	var $uses = array('User', 'Mailaddress', 'Transact');
	var $components = array('Pmaimai', 'RequestHandler', 'Name');
	
	function changepassword()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$bindusers = array(
			'hasOne' => array(
				'Mailaddress' =>  $this->User->hasOne_active['Mailaddress'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
		$this->set('error', false);
		
		if(!$this->data){
			return;
		}
		
		$result = true;
		if($this->data['Member']['newpw'] !== $this->data['Member']['confirmpw']){
			$this->_myErrorMsg[] = '入力と再入力のパスワードが違います';
			$result = false;
		}
		if(empty($this->data['Member']['password']) ||empty($this->data['Member']['newpw']) ||empty($this->data['Member']['confirmpw']) ){
			$this->_myErrorMsg[] = 'パスワード未入力の箇所があります';
			$result = false;
		}
		if(!$result){
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->User->set($user);
		if(!$this->User->isAuth($this->data['Member']['password'])){
			$this->_myErrorMsg[] = 'えらー';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Transact->begin();
		
		if(!$this->User->changePassword($this->data['Member']['newpw'])){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->User->validationErrors);
			return;
		}
		
		$sendData = array();
		$sendData[] = array(
			'mailaddress' => $user['Mailaddress']['mailaddress'],
			'viewpath' => '/email/password_change',
			'subject' => '【pmaimai】パスワード変更完了のお知らせ',
		);
		
		if(!$this->_sendMail($sendData)){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Transact->commit();
		$this->flash("パスワード変更完了＆完了メールを送信しました", '/');
	}
	
	function search()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		$this->set('user', $user);

	}
	
	function friend()
	{
		$this->Pmaimai->checkMemberAuth();
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		
		$bindusers = array(
			'hasMany' => array(
				'Friend' => $this->User->hasMany_active['Friend'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		$user = $this->User->findById($userID);
		
		$this->set('user', $user);
	}
	
	function viewfriend($id = null)
	{
		$this->Pmaimai->clean($id);
		$this->Pmaimai->checkMemberAuth();
		
		if(!$id || !is_numeric($id)){
			$this->redirect('/');
		}
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		
		$bindusers = array(
			'hasMany' => array(
				'Friend' => $this->User->hasMany_active['Friend'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		$user = $this->User->findByIdAndStatus($id, 'active');
		if(!$user){
			$this->flash("閲覧できません", '/');
			return;
		}
		//該当ユーザのプロフィールのコントロール範囲ここで　そのうち
		
		$this->set('user', $user);
	}
	
	function post()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$bindusers = array(
			'hasMany' => array(
				'Post' => $this->User->hasMany_active['Post'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
	
	}
	
	function message()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
	}
	
	function view($id = null)
	{
		$this->Pmaimai->clean($id);
		$this->Pmaimai->checkMemberAuth();
		
		if(!$id || !is_numeric($id)){
			$this->redirect('/');
		}
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		
		$user = $this->User->getOtherWithProfiles($userID, $id);
		if(!$user){
			$this->flash("閲覧できません", '/');
			return;
		}
		
		$this->set('user', $user);
		
	}
	
	function home()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
		
	}
	
	function logout(){
		$this->Pmaimai->checkMemberAuth();
		$this->Session->delete($this->sessionUserName);
		$this->redirect('/');
	}
	
	function profile()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$bindusers = array(
			'hasMany' => array(
				'Profile' => $this->User->hasMany_active['Profile'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
		
	}
	
	function setting()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$bindusers = array(
			'hasOne' => array(
				'Mailaddress' =>  $this->User->hasOne_active['Mailaddress'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
		
	}
	
	
	function resign()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$bindusers = array(
			'hasOne' => array(
				'Mailaddress' =>  $this->User->hasOne_active['Mailaddress'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
		$this->set('error', false);
		
		if(!$this->data){
			return;
		}
		
		if(empty($this->data['Member']['password'])){
			$this->_myErrorMsg[] = 'パスワードが未入力です';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		$this->User->set($user);
		
		if(!$this->User->isAuth($this->data['Member']['password'])){
			$this->_myErrorMsg[] = 'えらー';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Transact->begin();
		
		$result = true;
		
		if(!$this->User->updateStatus('resign')){
			$this->Transact->rollback();
			$this->_myErrorMsg[] = 'えらー';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Mailaddress->set($user);
		if(!$this->Mailaddress->updateStatus('resign')){
			$this->Transact->rollback();
			$this->_myErrorMsg[] = 'えらー';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$sendData = array();
		$sendData[] = array(
			'mailaddress' => $user['Mailaddress']['mailaddress'],
			'viewpath' => '/email/member_resign',
			'subject' => '【pmaimai】退会処理完了のお知らせ',
		);
		
		if(!$this->_sendMail($sendData)){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Transact->commit();
		$this->Session->delete($this->sessionUserName);
		$this->flash("退会処理が完了しました", '/');
		
	}
	
	
	
}
?>